package com.woniuxy.ui;

import com.woniuxy.entity.Student;
import com.woniuxy.service.StudentService;
import com.woniuxy.service.impl.StudentServiceImpl;

import java.util.List;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-09-18 9:33
 */
public class StudentMenu {
    static Scanner scanner = new Scanner(System.in);

    static StudentService studentService = new StudentServiceImpl();

    public static void show() {
        boolean flag = true;
        while (flag) {
            System.out.println("---------学生管理----------");
            System.out.println("1.添加学生");
            System.out.println("2.按姓名查询学生");
            System.out.println("3.查询所有学生");
            System.out.println("4.删除学生");
            System.out.println("5.修改学生");
            System.out.println("6.返回上一级");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    System.out.println("执行添加学生");
                    addStudent();
                    break;
                case 3:
                    System.out.println("显示所有学生");
                    showAllStudents();
                    break;
                case 4:
                    System.out.println("执行删除学生");
                    removeBySid();
                    break;
                case 6:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，重写输入");
                    break;
            }
        }
    }

    private static void removeBySid() {
        System.out.println("输入准备删除学生的学号");
        int sid = scanner.nextInt();
        try{
            studentService.removeBySid(sid);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    private static void showAllStudents() {
        List<Student> students = studentService.getAll();
        for(Student student : students) {
            System.out.println(student);
        }
    }

    private static void addStudent() {
        Student student = new Student();
        System.out.println("输入学号");
        student.setSid(scanner.nextInt());
        System.out.println("输入用户名");
        student.setSname(scanner.next());
        System.out.println("输入密码");
        student.setPassword(scanner.next());
        System.out.println("输入年龄");
        student.setAge(scanner.nextInt());
        System.out.println("输入邮箱");
        student.setEmail(scanner.next());
        System.out.println("输入手机号码");
        student.setPhone(scanner.next());
        //表示层调用业务逻辑层
        try {
            studentService.regist(student);
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
